#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 10010;
int n, m, ans;
int t[N], din[N], tot[N];
vector<int> e[N];

void toposort() {
	queue<int> q;
	for (int i = 1; i <= n; i++) {
		if (din[i] == 0) {
			tot[i] = t[i];
			q.push(i);
		}
	}
	while (q.size()) {
		int x = q.front(); q.pop();
		for (auto y : e[x]) {
			din[y]--;
			tot[y] = max(tot[y], tot[x] + t[y]);
			if (din[y] == 0) q.push(y);
		}
	}
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> t[i];
	for (int i = 1; i <= m; i++) {
		int x, y; cin >> x >> y;
		e[x].push_back(y);
		din[y]++;
	}
	toposort();
	for (int i = 1; i <= n; i++) ans = max(tot[i], ans);
	cout << ans << endl;
	return 0;
}